package com.lujianing.test;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import com.lujianing.test.util.PathUtil;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

import java.io.*;
import java.nio.charset.Charset;
import java.util.*;

/**
 * Created by lujianing on 2017/5/7.
 */
public class JavaToPdfHtmlVelocity {

    private static final String DEST = "target/HelloWorld_CN_HTML_VELOCITY.pdf";
    private static final String VM = "template_velocity.vm";
    private static final String FONT = "simhei.ttf";

    private static VelocityEngine engine = null;

    static {
        Properties properties = new Properties();
        properties.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, PathUtil.getCurrentPath());
        properties.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        properties.setProperty(RuntimeConstants.INPUT_ENCODING, "UTF-8");
        properties.setProperty(RuntimeConstants.OUTPUT_ENCODING, "UTF-8");
        engine = new VelocityEngine();
        engine.init(properties);
    }


    public static void main(String[] args) throws IOException, DocumentException {
        VelocityContext ctx = new VelocityContext();
        ctx.put("name","鲁家宁");
        String content = JavaToPdfHtmlVelocity.velocityRender(ctx,VM);
        JavaToPdfHtmlVelocity.createPdf(content,DEST);
    }


    public static void createPdf(String content,String dest) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
        // step 3
        document.open();
        // step 4
        XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
        fontImp.register(FONT);
        XMLWorkerHelper.getInstance().parseXHtml(writer, document,
                new ByteArrayInputStream(content.getBytes()), null, Charset.forName("UTF-8"), fontImp);
        // step 5
        document.close();

    }

    /**
     * velocity渲染html
     */
    public static String velocityRender(VelocityContext ctx, String htmlTmp) {
        Writer out = new StringWriter();
        try {
            // 获取模板
            Template template = engine.getTemplate(htmlTmp);
            // 合并数据模型与模板
            template.merge(ctx, out); //将合并后的数据和模板写入到流中，这里使用的字符流
            out.flush();
            return out.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return null;
    }
}
